// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129

module.exports = {
	_before: function() { // 通用预处理器
		this.dbJQL = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		})
		this.dbCmd = this.dbJQL.command
		this.$ = this.dbCmd.aggregate
	},
	/**
	 * 获取备考心得列表
	 */
	async getList(pageNo = 0, pageSize = 10, type = 0) {
		const dbJQL = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		})
		const dbCmd = dbJQL.command
		const $ = dbCmd.aggregate

		let reference = dbJQL.collection('circle').aggregate()
		if (type == 0) {
			reference = reference.match({
				flag: false
			})
		} else if (type == 1) {
			reference = reference.match({
				user_id: dbJQL.getCloudEnv('$cloudEnv_uid'),
				flag: false
			})
		}
		return await reference.sort('createTime desc')
			.lookup({
				from: 'uni-id-users',
				let: {
					user_id: '$user_id'
				},
				pipeline: $.pipeline().match(dbCmd.expr($.eq(['$_id', '$$user_id']))).done(),
				as: 'userInfo'
			})
			.project({
				content: 1,
				imageUrl: 1,
				view_count: 1,
				like_count: 1,
				createTime: 1,
				userInfo: $.arrayElemAt(['$userInfo', 0])
			})
			.skip((pageNo - 1) * pageSize)
			.limit(pageSize)
			.end()
	},
	/**
	 * 获取备考心得详情
	 */
	async getDetail(_id) {
		let res = await this.dbJQL.collection("circle").aggregate().match({
				_id
			})
			.lookup({
				from: 'uni-id-users',
				let: {
					user_id: "$user_id"
				},
				pipeline: this.$.pipeline().match(this.dbCmd.expr(this.$.eq(['$_id', '$$user_id'])))
					.project({
						nickname: 1,
						avatar: 1
					}).done(),
				as: 'userInfo'
			})
			.lookup({
				from: 'like',
				pipeline: this.$.pipeline().match(this.dbCmd.expr(
						this.$.and(
							this.$.eq(['$article_id', _id]),
							this.$.eq(['$article_type', 1]),
						)
					))
					.lookup({
						from: 'uni-id-users',
						let: {
							user_id: "$user_id"
						},
						pipeline: this.$.pipeline().match(
								this.dbCmd.expr(
									this.$.eq(['$_id', '$$user_id'])
								))
							.project({
								nickname: 1,
								avatar: 1
							}).done(),
						as: 'userInfo'
					})
					.project({
						userInfo: this.$.arrayElemAt(['$userInfo', 0]),
					})
					.done(),
				as: 'likes'
			})
			.lookup({
				from: 'like',
				pipeline: this.$.pipeline().match(
					this.dbCmd.expr(
						this.$.and([
							this.$.eq(['$article_type', 1]),
							this.$.eq(['$article_id', _id]),
							this.$.eq(['$user_id', this.dbJQL.getCloudEnv('$cloudEnv_uid')])
						])
					)
				).count('count').done(),
				as: 'is_like'
			})
			.lookup({
				from: 'favorite',
				let: {
					article_id: "$_id"
				},
				pipeline: this.$.pipeline().match(
					this.dbCmd.expr(
						this.$.and([
							this.$.eq(['$article_type', 1]),
							this.$.eq(['$article_id', "$$article_id"]),
							this.$.eq(['$user_id', this.dbJQL.getCloudEnv('$cloudEnv_uid')])
						])
					)
				).count('count').done(),
				as: 'is_favorite'
			})
			.project({
				userInfo: this.$.arrayElemAt(['$userInfo', 0]),
				content: 1,
				imageUrl: 1,
				view_count: 1,
				like_count: 1,
				createTime: 1,
				likes: 1,
				is_like: this.$.cond({
					if: this.$.gt([this.$.arrayElemAt(['$is_like.count', 0]), 0]),
					then: true,
					else: false
				}),
				is_favorite: this.$.cond({
					if: this.$.gt([this.$.arrayElemAt(['$is_favorite.count', 0]), 0]),
					then: true,
					else: false
				})
			})
			.end()
		if (res.errCode == 0) {
			res.data = res.data[0]
			// 获取review次数
			let result = await this.dbJQL.collection('review')
				.where(`article_id == '${res.data._id}' && user_id == $cloudEnv_uid && review_type == 1`).get()
			if (result.errCode == 0) {
				if (result.data.length > 0) {
					this.dbJQL.collection('review').doc(result.data[0]._id).update({
						create_date: this.dbJQL.getCloudEnv('$cloudEnv_now')
					})
				} else {
					this.dbJQL.collection('review').add({
						article_id: res.data._id,
						review_type: 1
					})
				}
			}
		}
		return res;
	},
	async circle_likes(id) {
		return this.dbJQL.collection('like').aggregate()
			.match({
				article_id: id,
				article_type: 1
			})
			.lookup({
				from: 'uni-id-users',
				let: {
					user_id: "$user_id"
				},
				pipeline: this.$.pipeline().match(
						this.dbCmd.expr(
							this.$.eq(['$_id', '$$user_id'])
						))
					.project({
						nickname: 1,
						avatar: 1
					}).done(),
				as: 'userInfo'
			})
			.project({
				userInfo: this.$.arrayElemAt(['$userInfo', 0]),
			})
			.end()
	},
	/** 发布备考心得 
	 * @property {String} content 心得内容
	 * @property {Array} imageUrl 附件
	 */
	async publish(params) {
		const dbJQL = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		})
		let reference = dbJQL.collection('circle')
		let {
			_id,
			...data
		} = params
		if (_id) {
			return await reference.doc(_id).update(data)
		} else {
			return await reference.add(data)
		}
	},
	async circle_detail(id) {
		const dbJQL = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		})
		return await dbJQL.collection('circle').doc(id).get({
			getOne: true
		})
	},
	async circle_delete(id) {
		const dbJQL = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		})
		return await dbJQL.collection('circle').doc(id).update({
			flag: true
		})
	}
}